/**
 * 风险警示书
 * Created by KY on 2017/8/18.
 */
define(['ldfw/core', 'art', 'text!tpl/product/risk_warn.tpl','comps/session',
        'comps/ajax', 'comps/record','comps/session','ldfw/utils/format', 'ldfw/helper/formatHelper','jquery-weui','ldfw/ldlibs/audio'],
    function (LDFW, art, tpl, session, ajax,record) {
        var View = LDFW.Container.extend({
            className: 'panel',
            tpl: tpl,
            events:{
                'click [operate="accept_risk_warn"]':'acceptRiskWarn',
                'click [name="readyRecord"]':'readyRecord'
            },
            initialize:function(){
                this.on('shown',this.loadData)
            },
            setProductNo:function(pd_no){
                this.pd_no = pd_no
            },
            rendered:function(){
              const _this = this
              this.record = record.init({
                  el:this.$el.find('button[name="readyRecord"]')
              })
              this.record.on('upload:success',function(resp){
                  _this.$el.find('[operate="accept_risk_warn"]').removeAttr('disabled')
                      .removeClass('weui-btn_disabled')
                  //返回文件序号
                  _this.file = resp
                  _this.file.fileName = '风险警示确认录音_'+new Date().getTime()+'.mp3'
                  _this.$el.find('button[name="readyRecord"]').text('重新录制')
                  ajax.preview('fileDownload',{fileId:_this.file.fileId})
                  .success(function(xhr){
                      let objectUrl = URL.createObjectURL(xhr.response);
                      if(_this.audio){
                          _this.audio.audioPlayer('updateSrc',objectUrl)
                      }else{
                          let audio = _this.$el.find('.record-audio > audio')[0]
                          audio.src = objectUrl
                          audio.onload = function(){
                              URL.revokeObjectURL(objectUrl);
                          }
                          _this.audio = $(audio).audioPlayer()
                      }
                  })
              })
            },
            acceptRiskWarn:function(){
                const _this = this
                let acceptRiskWarn = true
                $('input[type="radio"]').each(function(){
                    if($(this)[0].checked && $(this).val()!=1){
                        acceptRiskWarn = false
                        return
                    }
                })
                if(!acceptRiskWarn){
                    $.alert('对不起，您未对全部投资者声明进行确认，不允许签署风险揭示书')
                }else{
                    //留痕
                    ajax.post('cpliL.3.102',{
                        mark_type:11,
                        relation_field:session.getItem('time_stamp'),
                        markinfo_attach:_this.file.fileId+':'+_this.file.fileName,
                        pd_no:this.pd_no
                    }).success(function(){
                        LDFW.navigate('product/purchase/'+_this.pd_no)
                    })
                    //修改文件所属客户
                    ajax.post('pfL.9.102',{
                        pre_file_id_str:session.getItem('risk_warning_file')?session.getItem('risk_warning_file').split(':')[0]+';':'',
                        file_id_str:_this.file.fileId+';',
                        file_parent:2
                    }).success(function(){
                        session.setItem('risk_warning_file',_this.file.fileId+':'+_this.file.fileName)
                    })
                }
            },
            readyRecord:function(){
                let audio = this.$el.find('audio')[0]
                if(audio && !audio.paused){
                    audio.pause()
                }
            }
        })
        return new View()
    })